package com.example.xwweibo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xwcommon.entity.Weibo;
import com.example.xwcommon.service.WeiboService;
import com.example.xwcommon.vo.Response;
import com.example.xwweibo.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "weiboService")
public class WeiboServiceImpl implements WeiboService {
    @Autowired
    private WeiboMapper weiboMapper;
    @Override
    @Transactional
    public Response<Weibo> addWeibo(String userName, String title, String content,String img) {
        try{
            Weibo weibo = new Weibo();
            weibo.setUserName(userName);
            weibo.setTitle(title);
            weibo.setContent(content);
            weibo.setCreateTime(new java.util.Date());
            weibo.setImg(img);
            weibo.setReadCount(0);
            weibo.setState(2);
            if(weiboMapper.insert(weibo)==1){
                return new Response<>(200,"发表微博成功",weibo);
            }
            return new Response<>(500,"发表失败",null);
        }
        catch(Exception e){
            return new Response<>(500,e.getMessage(),null);
        }
    }

    @Override
    public Response<Weibo> getWeiboById(Integer weiboId) {
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wb_id",weiboId);
        Weibo weibo = weiboMapper.selectOne(queryWrapper);
        if(weibo!=null){
            return new Response<>(200,"查询成功",weibo);
        }
        return new Response<>(500,"查询失败,微博不存在",null);
    }


    @Override
    public Response<Page<Weibo>> getWeiboListByLoginName(Integer pageNum, Integer pageSize, String loginName, Integer state) {
        Page<Weibo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wb_user_loginname",loginName);
        if(state!=null){
            queryWrapper.eq("wb_state",state);
        }
        Page<Weibo> weiboPage = weiboMapper.selectPage(page, queryWrapper);
        if(weiboPage!=null){
            return new Response<>(200,"查询成功",weiboPage);
        }
        return new Response<>(500,"查询失败",null);
    }

    @Override
    public Response<List<Weibo>> getWeiboListByState(Integer state) {
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wb_state",state);
        List<Weibo> weiboList = weiboMapper.selectList(queryWrapper);
        if(weiboList!=null){
            return new Response<>(200,"查询成功",weiboList);
        }
        return new Response<>(500,"查询失败",null);
    }

    @Override
    public Response<Page<Weibo>> list(Integer pageNum, Integer pageSize, String findtxt, Integer state) {
        Page<Weibo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        if(findtxt!=null){
            queryWrapper.or(wrapper -> wrapper.like("wb_title",findtxt).or().like("wb_content",findtxt));
        }
        if(state!=null){
            queryWrapper.eq("wb_state",state);
        }
        Page<Weibo> weiboPage = weiboMapper.selectPage(page, queryWrapper);
        if(weiboPage!=null){
            return new Response<>(200,"查询成功",weiboPage);
        }
        return new Response<>(500,"查询失败",null);
    }

    @Override
    @Transactional
    public Response<Weibo> updateState(Integer weiboId, Integer state) {
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wb_id",weiboId);
        Weibo weibo = weiboMapper.selectOne(queryWrapper);
        if(weibo==null){
            return new Response<>(500,"微博不存在",null);
        }
        weibo.setState(state);
        if(weiboMapper.updateById(weibo)==1){
            return new Response<>(200,"修改成功",weibo);
        }
        return new Response<>(500,"修改微博状态失败",null);
    }

    @Override
    @Transactional
    public Response<Weibo> updateReadCount(Integer weiboId) {
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wb_id",weiboId);
        Weibo weibo = weiboMapper.selectOne(queryWrapper);
        weibo.setReadCount(weibo.getReadCount()+1);
        if(weiboMapper.updateById(weibo)==1){
            return new Response<>(200,"修改成功",weibo);
        }
        return new Response<>(500,"修改微博阅读数失败",null);
    }

    @Override
    public Response<Integer> getWeiboCountByLoginName(String loginName, Integer state) {
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wb_user_loginname",loginName);
        queryWrapper.eq("wb_state",state);
        List<Weibo> weibos = weiboMapper.selectList(queryWrapper);
        if(!weibos.isEmpty()){
            return new Response<>(200,"获取总数成功",weibos.size());
        }
        return new Response<>(500,"获取总数失败",null);
    }

    @Override
    @Transactional
    public Response<Weibo> deleteWeibo(Integer weiboId) {
        QueryWrapper<Weibo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wb_id",weiboId);
        if(weiboMapper.delete(queryWrapper)==1){
            return new Response<>(200,"删除成功",null);
        }
        return new Response<>(500,"删除失败",null);
    }


}
